/**
 * @author Tobias Boehm
 *
 * 24.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.anddev.andengine.extension.tbsengine.match.playerslot.AbsPlayerSlot.SlotState;
import org.anddev.andengine.extension.tbsengine.match.playerslot.BaseServerPlayerSlot;

/**
 * This is an abstract class for a server message which should be used to
 * announce the change of a player slot to all players connected to a match
 * preparation.
 * <p>
 * With this message you should provide all necessary information to rebuild
 * the new state of the corresponding player slot.
 * 
 * 
 * @author Tobias Boehm
 *
 */
public abstract class AbsServerMatchpreparationSlotChangedAnnouncementMessage extends
		AbsServerMatchpreparationSlotMessage {

	protected UUID mPlayerID = null;
	protected SlotState mSlotState = SlotState.CLOSED;
	protected int mTeamID = -1;
	protected String mFactionID = "";
	

	protected abstract void onReadTransmissonMatchSlotAdditionalData(final DataInputStream pDataInputStream) throws IOException;
	protected abstract void onWriteTransmissonMatchSlotAdditionalData(final DataOutputStream pDataOutputStream) throws IOException;

	
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCHPREPARATION_SLOT_CHANGE_ANNOUNCEMENT;
	}
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.server.messages.preparematch.AbsServerMatchSlotMessage#onReadTransmissonMatchSlotData(java.io.DataInputStream)
	 */
	@Override
	protected void onReadTransmissonMatchSlotData(
			DataInputStream pDataInputStream) throws IOException {
		
		this.mPlayerID = UUID.fromString(pDataInputStream.readUTF());
		
		this.mSlotState = SlotState.valueOf(pDataInputStream.readUTF());
		
		this.mTeamID = pDataInputStream.readInt();
		
		this.mFactionID = pDataInputStream.readUTF();
		
		this.onReadTransmissonMatchSlotAdditionalData(pDataInputStream);
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.server.messages.preparematch.AbsServerMatchSlotMessage#onWriteTransmissonMatchSlotData(java.io.DataOutputStream)
	 */
	@Override
	protected void onWriteTransmissonMatchSlotData(
			DataOutputStream pDataOutputStream) throws IOException {

		pDataOutputStream.writeUTF(this.mPlayerID.toString());
		
		pDataOutputStream.writeUTF(this.mSlotState.name());
		
		pDataOutputStream.writeInt(this.mTeamID);
		
		pDataOutputStream.writeUTF(this.mFactionID);
		
		this.onWriteTransmissonMatchSlotAdditionalData(pDataOutputStream);
	}
	
	@Override
	public void setSlot(final BaseServerPlayerSlot pSlot) {
		super.setSlot(pSlot);
		
		if(pSlot.hasPlayer()){
			
			this.mPlayerID = pSlot.getPlayer().getPlayerID();
		}
		
		this.mSlotState = SlotState.valueOf(pSlot.getPlayerSlotState().name());
		
		this.mTeamID = pSlot.getTeam().getTeamID();
		
		this.mFactionID = pSlot.getFaction().getFactionIDname();
	}

	/**
	 * @return the mPlayerID
	 */
	public UUID getPlayerID() {
		return mPlayerID;
	}

	/**
	 * @return the mSlotState
	 */
	public SlotState getSlotState() {
		return mSlotState;
	}

	/**
	 * @return the mTeamID
	 */
	public int getTeamID() {
		return mTeamID;
	}

	/**
	 * @return the mFactionID
	 */
	public String getFactionID() {
		return mFactionID;
	}

}
